﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;

namespace Logistics
{
    public partial class frmUsersModify : Form
    {
        public delegate void ModelEventHandler(Model.tc_User model);
        public ModelEventHandler OnModelEvent;
        private Model.tc_User User;
        public frmUsersModify()
        {
            InitializeComponent();
        }
        public frmUsersModify(Model.tc_User _user)
        {
            InitializeComponent();
            User = _user;
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            if (!VerifyFrom())
                return;
            var model = new Model.tc_User();
            model.UserName = txtUserName.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.DepartmentId = Guid.Parse(cmbDepartment.SelectedValue.ToString());
            model.RoleId = Guid.Parse(cmbRole.SelectedValue.ToString());
            model.Password = txtPassword.Text.Trim();
            if (string.IsNullOrWhiteSpace(model.Password))
                model.Password = "123456";
            model.IsActive = cbIsActive.Checked;
            var msg = "确定要保存'" + txtName.Text + "'吗？";
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                var objUser = new Manager.tc_User();
                if (User == null)
                {
                    model.Id = Guid.NewGuid();
                    objUser.Add(model);
                }
                else
                {
                    model.Id = User.Id;
                    model.Department = (cmbDepartment.SelectedItem as Model.tc_Department).Department;
                    model.Role = (cmbRole.SelectedItem as Model.tc_Role).Role;
                    objUser.Update(model);
                }
                OnModelEvent?.Invoke(model);
                MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
        private bool VerifyFrom()
        {
            var msg = string.Empty;
            if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                msg = "用户名不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                msg = "人员名称不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }

            if (cmbDepartment.SelectedIndex == 0)
            {
                msg = "请选择部门";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }

            if (cmbRole.SelectedIndex == 0)
            {
                msg = "请选择角色";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }
            return true;
        }
        private void butCancel_Click(object sender, EventArgs e)
        {
            var msg = "数据尚未保存，确认要取消吗？";
            if (MessageBox.Show(msg, "警告", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void frmUsersModify_Load(object sender, EventArgs e)
        {
            BindCmbData();
            if (User != null)
                DataBind();
        }
        private void DataBind()
        {
            txtUserName.Text = User.UserName;
            txtName.Text = User.Name;
            cmbDepartment.SelectedValue = User.DepartmentId;
            cmbRole.SelectedValue = User.RoleId;
            txtPassword.Text = User.Password;
            cbIsActive.Checked = User.IsActive;
        }
        #region 绑定基础数据
        private void BindCmbData()
        {
            var objDepartment = new Manager.tc_Department();
            var list = objDepartment.GetList();
            var model = new Model.tc_Department();
            model.Department = "请选择";
            model.Id = Guid.Empty;
            list.Insert(0, model);
            cmbDepartment.DataSource = list;
            cmbDepartment.DisplayMember = "Department";
            cmbDepartment.ValueMember = "Id";
            if (cmbDepartment.Items.Count > 1)
                cmbDepartment.SelectedIndex = 1;
            else
                cmbDepartment.SelectedIndex = 0;

            var objRole = new Manager.tc_Role();
            var rlist = objRole.GetList();
            var role = new Model.tc_Role();
            role.Role = "请选择";
            role.Id = Guid.Empty;
            rlist.Insert(0, role);
            cmbRole.DataSource = rlist;
            cmbRole.DisplayMember = "Role";
            cmbRole.ValueMember = "Id";
            if (cmbRole.Items.Count > 1)
                cmbRole.SelectedIndex = 1;
            else
                cmbRole.SelectedIndex = 0;
        }
        #endregion
    }
}
